<?php
/**
*+-------关注中心操作类------------------------+
*+-------------------------------------------------+
*+----关注视频,用户-------------------------------+
*+-------------------------------------------------+
*+-------------------------------------------------+
*+-@author susy.liang <liangling@ilongyuan.com.cn>-+
*+-------------------------------------------------+
*/

namespace Home\Controller;

class FollowController extends BaseUserController {

	/**
	*错误操作信息
	*/
	protected $errInfo = '';

	protected function _initialize(){
		parent::_initialize();
	}


	/**
	*用户关注
	*/
	public function doFollow(){

		$data = I('');
		$data['uid'] = intval($data['uid']);

		if(false === $this->checkParam($data))
			$this->ajaxReturn(array('state'=>-1, 'info'=>$this->errInfo));

		$eventObj = A('Api/Follow', 'Event');
		$res = $eventObj->follow(UID, $data['uid']);
		if (!$res){

			$this->ajaxReturn(array('state'=>-1, 'info'=>'关注失败'));

		}else{

			$total = intval($eventObj->count_follows($data['uid']));

			$this->ajaxReturn(array('state'=>0, 'info'=>'关注成功', 'data'=>array('total'=>$total)));
		}
	}

	/**
	*用户取消关注
	*/
	public function unFollow(){

		$data = I('');
		$data['uid'] = intval($data['uid']);

		if(false === $this->checkParam($data))
			$this->ajaxReturn(array('state'=>-1, 'info'=>$this->errInfo));

		$eventObj = A('Api/Follow', 'Event');
		$res = $eventObj->unfollow(UID, $data['uid']);
		if (!$res){

			$this->ajaxReturn(array('state'=>-2, 'info'=>'未关注不能取消'));

		}else{

			$total = intval($eventObj->count_follows($data['uid']));

			$this->ajaxReturn(array('state'=>0, 'info'=>'取消成功', 'data'=>array('total'=>$total)));
		}
	}

	/**
	 * 移除粉丝
	 * @author liubo <liubo@ilongyuan.com.cn>
	 */
	public function removeFans() {
		$uid = I('uid', 0, 'intval');
		if (empty($uid)) {
			$this->ajaxReturn(array('state'=>-1, 'info'=>'参数错误'));
		}
		$eventObj = A('Api/Follow', 'Event');
		$res = $eventObj->unfollow($uid, UID);
		if (!$res) {
			$this->ajaxReturn(array('state'=>-1, 'info'=>'参数错误'));
		}
		$this->ajaxReturn(array('state'=>0, 'info'=>'操作成功'));
	}

	/**
	*关注参数的检查
	*/
	private function checkParam($data){

		if(!$data['uid']){
			$this->errInfo = '参数错误';
			return false;
		}

		if($data['uid'] == UID){
			$this->errInfo = '不能关注本人';
			return false;
		}

		$uinfo = A('Api/User', 'Event')->info($data['uid']);
		if (!count($uinfo)){
			$this->errInfo = '关注用户不合法';
			return false;
		}

		return true;

	}

}
